package net.jirox.staticengine.upload;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.fileupload.FileItem;

@SuppressWarnings("serial")
public class MyFileItem implements FileItem {

	private String fieldName;
	private String contentType;
	private boolean isFormField;
	private String fileName;
	private ByteArrayOutputStream outputStream;

	public MyFileItem(String fieldName, String contentType, boolean isFormField, String fileName) {
		this.fieldName = fieldName;
		this.contentType = contentType;
		this.isFormField = isFormField;
		this.fileName = fileName;
	}

	public void delete() {
		outputStream = null;
	}

	public byte[] get() {
		if (outputStream == null) { return null; }
		return outputStream.toByteArray();
	}

	public String getContentType() {
		return contentType;
	}

	public String getFieldName() {
		return fieldName;
	}

	public InputStream getInputStream() throws IOException {
		return null;
	}

	public String getName() {
		return fileName;
	}

	public OutputStream getOutputStream() throws IOException {
		if (outputStream == null) {
			outputStream = new ByteArrayOutputStream();
		}
		return outputStream;
	}

	public long getSize() {
		return outputStream.size();
	}

	public String getString() {
		return new String(get());
	}

	public String getString(String arg0) throws UnsupportedEncodingException {
		return null;
	}

	public boolean isFormField() {
		return isFormField;
	}

	public boolean isInMemory() {
		return true;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public void setFormField(boolean isFormField) {
		this.isFormField = isFormField;
	}

	public void write(File file) throws Exception {
		// do not write
		return;
	}

}
